var t = require("../../../@babel/runtime/helpers/defineProperty"), a = getApp();

Page({
    data: {
        categoryId: 0,
        parentId: 0,
        categoryData: {},
        parentData: {},
        display_order: 0,
        name: "",
        is_home: 0,
        enabled: 0,
        thumb_src: ""
    },
    onLoad: function(t) {
        t.categoryId && 0 != t.categoryId && (this.setData({
            categoryId: t.categoryId
        }), this.getData(t.categoryId)), t.parentId && 0 != t.parentId && (this.setData({
            parentId: t.parentId
        }), this.getData(t.parentId));
    },
    onReady: function() {},
    updateGoodsImg: function(t) {
        this.data.thumb_src = t.detail[0] ? t.detail[0] : "";
    },
    onShow: function() {},
    switchChange: function(a) {
        var e = a.currentTarget.dataset.name;
        this.setData(t({}, e, a.detail));
    },
    getData: function(t) {
        var e = this, r = a.getNetAddresss("plugin.shop-assistant.frontend.category.detail");
        a._postNetWork({
            url: r,
            data: {
                id: t
            },
            success: function(t) {
                var r = t.data;
                if (1 != r.result) return a.tips(r.msg);
                e.data.categoryId && e.data.categoryId > 0 ? e.setData({
                    categoryData: r.data,
                    display_order: r.data.display_order,
                    name: r.data.name,
                    thumb_src: r.data.thumb_src,
                    is_home: r.data.is_home,
                    enabled: r.data.enabled
                }) : e.setData({
                    parentData: r.data
                });
            }
        });
    },
    sureEdit: function() {
        if (a._isTextEmpty(this.data.name)) return a.tips("分类名称不能为空");
        a._isTextEmpty(this.data.categoryId) ? this.addCategory() : this.editCategory();
    },
    editCategory: function() {
        var t = {}, e = this.data.categoryData, r = e.level, n = e.parent_id, s = this.data, d = s.name, i = s.thumb_src, o = s.display_order, c = s.is_home, u = s.enabled;
        t.category = {
            level: r,
            parent_id: n,
            name: d,
            display_order: o,
            is_home: c,
            enabled: u,
            thumb: i
        }, t.id = this.data.categoryId;
        var g = a.getNetAddresss("plugin.shop-assistant.frontend.category.update");
        a._postNetWork({
            url: g,
            data: t,
            success: function(t) {
                var e = t.data;
                if (1 != e.result) return a.tips(e.msg);
                var r = getCurrentPages(), n = r[r.length - 2];
                n.initData(), n.getData(), wx.navigateBack();
            }
        });
    },
    addCategory: function() {
        var t = {}, e = this.data, r = e.name, n = e.thumb_src, s = e.display_order, d = e.is_home, i = e.enabled;
        t.category = {
            name: r,
            display_order: s,
            is_home: d,
            enabled: i,
            level: this.data.parentData.level ? this.data.parentData.level + 1 : 1,
            thumb: n,
            parent_id: this.data.parentId
        };
        var o = a.getNetAddresss("plugin.shop-assistant.frontend.category.create");
        a._postNetWork({
            url: o,
            data: t,
            success: function(t) {
                var e = t.data;
                if (1 != e.result) return a.tips(e.msg);
                var r = getCurrentPages(), n = r[r.length - 2];
                n.initData(), n.getData(), wx.navigateBack();
            }
        });
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {}
});